home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / PRGMANIA / BFED.10 / EDIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-27  |  7.2 KB  |  368 lines

  1. /*
  2.     file: edit.c
  3.     utility:
  4.     date: 1989
  5.     author: Jim Charlton
  6.     modifications:
  7.         1995: C. Moreau: 
  8.     comments: 
  9. */
  10. #include <stddef.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include "e:\proging\c\libs\malib\alert.h"
  16.  
  17. #include "bufman.h"
  18. #include "edit.h"
  19. #include "menu.h"
  20. #include "onepage.h"
  21. #include "send.h"
  22.  
  23. #include "bfed_rsc.h"
  24.  
  25. /* 
  26.     globals vars
  27. */
  28. int    ins    = 0;
  29. int    inhex = 0;
  30.  
  31. /*
  32.     name: delete_one
  33.     utility: delete one byte
  34.     comment: 
  35.     parameters:
  36.     return:
  37.     date: 1989
  38.     author: Jim Charlton
  39.     modifications:
  40.         1995: C. Moreau: 
  41. */
  42. void delete_one(windowptr thewin, long pos, int flag)
  43. {
  44.         /*    flag is 0 when saving the file in which case can delete last byte */
  45.     if ( (thewin->position==thewin->flen-1) && flag)
  46.         rsc_alert(LAST_BYTE);
  47.     else
  48.     {
  49.         linkbufptr amem = thewin->headptr;
  50.         char *addr, *addr2;
  51.         
  52.         while (amem->inuse <= pos)
  53.         {
  54.             pos -= amem->inuse;
  55.             amem = amem->next;
  56.         }
  57.         addr = (char *)(amem->block+pos);
  58.         for (addr2=addr;addr2<(char *)amem->block+amem->inuse;addr2++)
  59.             *addr2 = *(addr2+1);  
  60.     
  61.         amem->inuse--;
  62.         thewin->flen--; 
  63.         if (thewin->position >= thewin->flen) 
  64.             thewin->position = thewin->flen-1; 
  65.         if (flag)    /* don't set to partial if deleting last byte in file    */
  66.             thewin->changed = TRUE;
  67.     }
  68. }
  69.  
  70. /*
  71.     name: cutit
  72.     utility: cut out the marked text
  73.     comment: 
  74.     parameters:
  75.     return:
  76.     date: 1989
  77.     author: Jim Charlton
  78.     modifications:
  79.         1995: C. Moreau: 
  80. */
  81. void cutit(windowptr    thewin)
  82. {
  83.     linkbufptr amem;
  84.     long pos = thewin->startmark;
  85.     long togo = thewin->endmark - thewin->startmark + 1;
  86.     int    inthis;
  87.     
  88.     if (pos <= thewin->flen)
  89.     {
  90.         amem = thewin->headptr;
  91.         while (amem->inuse <= pos)
  92.         {
  93.             pos -= amem->inuse;
  94.             amem = amem->next;
  95.         }
  96.         inthis = (int)(amem->inuse - pos);
  97.     }
  98.     
  99.     if (togo > 0L)
  100.     {    
  101.         while (inthis < togo)
  102.         {    
  103.             amem->inuse = pos;
  104.             if (!amem->inuse)
  105.                 dispose_member(thewin,amem);
  106.             amem = amem->next;
  107.             togo -= inthis;
  108.             thewin->flen -= inthis;
  109.             inthis = (int)(amem->inuse);
  110.             pos = 0L;
  111.         }
  112.         
  113.         if (togo > 0L)
  114.         {
  115.             memmove( amem->block+pos, amem->block+pos+togo,
  116.                      amem->inuse-togo-pos );
  117.             amem->inuse -= togo;
  118.             thewin->flen -= togo;
  119.         }
  120.     }
  121.     
  122.     thewin->position = thewin->startmark;
  123.     if (thewin->topchar >= thewin->position) 
  124.         thewin->topchar = thewin->position&~0x7;    
  125. }
  126.  
  127. /*
  128.     name: copy
  129.     utility: copy the marked text to the cutbuffer
  130.     comment: 
  131.     parameters:
  132.     return:
  133.     date: 1989
  134.     author: Jim Charlton
  135.     modifications:
  136.         1995: C. Moreau: 
  137. */
  138. void copy(windowptr    thewin)
  139. {
  140.     linkbufptr    amem;
  141.     long        pos = thewin->startmark;
  142.     char        *addr;
  143.  
  144.     if (pos <= thewin->flen)
  145.     {
  146.         amem = thewin->headptr;
  147.         while (amem->inuse <= pos)
  148.         {
  149.             pos -= amem->inuse;
  150.             amem = amem->next;
  151.         }
  152.         addr = (char *)(amem->block+pos);
  153.     }
  154.     
  155.     if (cutbuffer)
  156.         free(cutbuffer);
  157.  
  158.     cutlength = thewin->endmark - thewin->startmark +1;
  159.  
  160.     if (cutlength > 0) /* bail out if end is before start */
  161.     {
  162.         cutbuffer = malloc((unsigned)(cutlength+2));
  163.         if (cutbuffer)
  164.         {
  165.             long tocopy = cutlength;
  166.             int bufbytes = (int)(amem->inuse - pos);
  167.             char *addr2 = cutbuffer;
  168.         
  169.             while(tocopy > 0)
  170.             {
  171.                 int    num = ((int)tocopy < bufbytes) ? (int)tocopy : bufbytes;
  172.  
  173.                 memcpy( addr2, addr, (long)num );
  174.                 tocopy -= bufbytes;
  175.                 if (tocopy > 0)
  176.                 {
  177.                     amem = amem->next;
  178.                     addr = amem->block;
  179.                     bufbytes = (int)(amem->inuse);
  180.                     addr2 += num;
  181.                 }    
  182.             }
  183.             *(cutbuffer + cutlength ) = 0;
  184.         }
  185.         else
  186.             rsc_alert(NOMEM_BUF);
  187.     }
  188. }        
  189.  
  190. /*
  191.     name: paste
  192.     utility:     paste the cutbuffer into the file before the cursor
  193.     comment: 
  194.     parameters:
  195.     return:
  196.     date: 1989
  197.     author: Jim Charlton
  198.     modifications:
  199.         1995: C. Moreau: 
  200. */
  201. void paste(windowptr    thewin)
  202. {
  203.     char *modeptr;
  204.  
  205.         /* paste will replace/overwrite if ins = FALSE otherwise inserts */
  206.     modeptr = (ins?modein:moderp) + 2;    /* skip the 2 spaces */
  207.         
  208.     if (rsc_falert(PASTEMODE, modeptr) != 2)
  209.     {
  210.         if( !ins )
  211.         {  /* cut out cutlength bytes */
  212.             thewin->startmark = thewin->position;
  213.             thewin->position += cutlength;
  214.             thewin->position = thewin->position<thewin->flen-1 ?
  215.                                  thewin->position : thewin->flen-1;
  216.             thewin->endmark = thewin->position -1;
  217.             thewin->markson = TRUE;
  218.             cutit(thewin);
  219.             thewin->changed = TRUE;
  220.                 /* following code just clears the marks  */
  221.             thewin->startmark = 1;
  222.             thewin->endmark = 0;
  223.             thewin->markson = FALSE;
  224.         }
  225.         insert_it( thewin, cutlength, cutbuffer );
  226.         send_vslid(thewin);
  227.         send_redraw(thewin);
  228.     }
  229. }
  230.  
  231. /*
  232.     name: insert_it
  233.     utility: insert_it() inserts incopy bytes from string pointed to by addr2
  234.     into the file before the position of the cursor 
  235.     comment: 
  236.     parameters:
  237.     return:
  238.     date: 1989
  239.     author: Jim Charlton
  240.     modifications:
  241.         1995: C. Moreau: 
  242. */
  243. void insert_it(windowptr thewin, long incopy, char *addr2)
  244. {
  245.     long pos;
  246.     long insertpos = thewin->position;
  247.     long tocopy = incopy;
  248.     linkbufptr    amem;
  249.     char *posaddr,*addr;
  250.     int    havenow, num, tomoveup;
  251.     
  252.     while (tocopy > 0)
  253.     {
  254.             /* calculate amem that cursor is in */
  255.         pos = insertpos;
  256.         amem = thewin->headptr;
  257.         while (amem->inuse < pos)
  258.         {
  259.             pos -= amem->inuse;
  260.             amem = amem->next;
  261.         }
  262.         posaddr = (char *)(amem->block+pos);
  263.  
  264.         if (amem->inuse == BLOCKSIZE)
  265.         {
  266.                 /* free up some space for the insert */
  267.             if (insert_member(amem))  /* no more memory available  */
  268.             {
  269.                 rsc_alert(NOMEM_1);
  270.                  break;
  271.             }
  272.                 /* recalculate pos and amem */
  273.             pos = insertpos;
  274.             amem = thewin->headptr;
  275.             while (amem->inuse <= pos)
  276.             {
  277.                 pos -= amem->inuse;
  278.                 amem = amem->next;
  279.             }
  280.             posaddr = (char *)(amem->block+pos);
  281.         }
  282.     /* pos is now position of cursor in number of bytes from amem->block */
  283.  
  284.         addr = posaddr;
  285.         havenow = BLOCKSIZE - (int)amem->inuse;
  286.         num = ((int)tocopy < havenow) ? (int)tocopy : havenow;
  287.         for(tomoveup = (int)(amem->inuse - pos-1);(int)tomoveup>-2;tomoveup--) 
  288.             *(addr+tomoveup+num) = *(addr+tomoveup);
  289.         memmove( addr, addr2, (long)num );
  290.         amem->inuse += num;
  291.         addr2 += num;
  292.         insertpos += num;    
  293.         tocopy -= num;
  294.     }
  295.     thewin->flen += incopy;
  296.     thewin->changed = TRUE;
  297. }
  298.  
  299. /*
  300.     name: start_mark
  301.     utility: 
  302.     comment: 
  303.     parameters:
  304.     return:
  305.     date: 1989
  306.     author: Jim Charlton
  307.     modifications:
  308.         1995: C. Moreau: 
  309. */
  310. void start_mark(windowptr thewin)
  311. {
  312.     thewin->startmark = thewin->position;
  313.  
  314.     if( (thewin->startmark <= thewin->endmark)
  315.             || thewin->markson )
  316.     {
  317.         send_redraw(thewin);
  318.         thewin->markson = TRUE;
  319.         update_menu();
  320.     }    
  321. }
  322.  
  323. /*
  324.     name: end_mark
  325.     utility: 
  326.     comment: 
  327.     parameters:
  328.     return:
  329.     date: 1989
  330.     author: Jim Charlton
  331.     modifications:
  332.         1995: C. Moreau: 
  333. */
  334. void end_mark(windowptr    thewin)
  335. {    
  336.     thewin->endmark = thewin->position<thewin->flen-1 ?
  337.                              thewin->position : thewin->flen-2;
  338.  
  339.     if( (thewin->endmark >= thewin->startmark)
  340.             || thewin->markson )
  341.     {
  342.         send_redraw(thewin);
  343.         thewin->markson = TRUE;
  344.         update_menu();
  345.     }
  346. }
  347.  
  348. /*
  349.     name: clear_marks
  350.     utility: 
  351.     comment: 
  352.     parameters:
  353.     return:
  354.     date: 1989
  355.     author: Jim Charlton
  356.     modifications:
  357.         1995: C. Moreau: 
  358. */
  359. void clear_marks(windowptr    thewin)
  360. {
  361.     thewin->startmark = 1;
  362.     thewin->endmark = 0;
  363.     send_redraw(thewin);
  364. /*    one_page(thewin); */
  365.     thewin->markson = FALSE;
  366.     update_menu();
  367. }
  368.